﻿using Bear.Library.Util;
using Emgu.CV;
using Emgu.CV.Structure;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Bear.Models
{
    [Serializable]
    public class Player
    {
        public Player()
        {
            this.Skills = new List<Skill>();
            //  this.Life = MapleStory.GetLifeBar();
            //  this.Magic = MapleStory.GetMagicBar();
        }

        public string DisplayString
        {
            get
            {
                return this.AccountName + "_" + this.Role;
            }
        }

        //标识1
        public string MarkName1 { get; set; }
        //标识2
        public string MarkName2 { get; set; }

        public Image<Gray, byte> Mark1 { get; set; }

        public Image<Gray, byte> Mark2 { get; set; }

        public int Level { get; set; }
        public Jobs Job { get; set; }

        public string AccountName { get; set; }
        public string Password { get; set; }
        public string SecurityCode { get; set; }

        public int Role { get; set; }

        public LifeAndMagic Life { get; set; }
        public LifeAndMagic Magic { get; set; }
        public List<Skill> Skills { get; set; }

        public int SkillRange { get; set; }
        public int SkillDirection { get; set; }


        public void Save()
        {

            string dir = Path.Combine(Players.DIRECTORY_PATH, this.AccountName + "_" + this.Role);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            SerializerHelper.XmlSerializer(this, Path.Combine(dir, "Config.xml"));
        }



    }

    [Serializable]
    public class Players : List<Player>
    {
        public const string DIRECTORY_PATH = @"Datas\Players";
        public const string POINT_FILE_PATH = @"Datas\MapPoint.png";


        public static Players Load()
        {

            Image<Bgr, byte> mapPoint = new Image<Bgr, byte>(POINT_FILE_PATH);

            Players players = new Players()
            {
                MapPoint = new Image<Bgr, byte>(POINT_FILE_PATH)
            };

            if (!Directory.Exists(DIRECTORY_PATH))
            {
                Directory.CreateDirectory(DIRECTORY_PATH);
            }

            //获取用户列表
            foreach (var dir in Directory.GetDirectories(DIRECTORY_PATH))
            {
                Player player = SerializerHelper.XmlDeserialize<Player>(Path.Combine(dir, "Config.xml"));

                players.Add(player);
            }
            return players;

        }

        public Image<Bgr, byte> MapPoint { get; set; }

        public void Save()
        {
            foreach (var dir in Directory.GetDirectories(DIRECTORY_PATH))
            {
                if (this.Count(t => t.AccountName + "_" + t.Role == Path.GetDirectoryName(dir)) <= 0)
                {
                    Directory.Delete(dir, true);
                }
            }

            foreach (var item in this)
            {
                item.Save();
            }
        }

    }
}
